/**
 * 
 */
package com.desksoft.ecommerce.helper.mall;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.lang.StringUtils;

import com.desksoft.ecommerce.constants.mall.Structure;
import com.desksoft.ecommerce.vo.KeyAndValue;
import com.google.common.io.Files;

/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: Feb 6, 2012
 * 创建作者：yejw
 * 文件名称：JAXBAnalysisHelper.java
 * 版本：1.0
 * 功能：
 * 最后修改时间：Feb 6, 2012
 * 修改记录：
 * </pre>
 *****************************************/
public class JAXBAnalysisHelper {
	
	/**
	 * 加载SRC目录下配置信息
	 * 
	 * @param <T>
	 * @param contextPath		如: com.desksoft.utility.ui.config.jaxb
	 * @param relativePath		如: /com/desksoft/utility/ui/config/configuration.xml
	 * @return
	 */
	public static <T> T loadConfiguration(String contextPath, String relativePath) {
		checkArgument(StringUtils.startsWith(relativePath, "/"), "XML文件必须以 '/' 开头.");
		return JAXBAnalysisHelper.<T>loadConfiguration(contextPath, JAXBAnalysisHelper.class.getResourceAsStream(relativePath));
	}
	
	/**
	 * 根据输入流解析配置信息
	 * 
	 * @param <T>
	 * @param contextPath		如: com.desksoft.utility.ui.config.jaxb
	 * @param is
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T loadConfiguration(String contextPath, InputStream is) {
		checkNotNull(is);
		checkNotNull(contextPath);
		Unmarshaller unmarshaller = null;
		
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(contextPath);
			unmarshaller = jaxbContext.createUnmarshaller();
		
			Object o = unmarshaller.unmarshal(is);
			return (T) o;
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 加载配置信息
	 * 
	 * @param <T>
	 * @param structure						结构类型
	 * @param folderRelativePath			相对路径 
	 * 				组件: "/03.DESKSOFT_MALL/html/pages/pages/structure/component/" 后的相对路径, 如 common/testcom
	 * 				布局: "/03.DESKSOFT_MALL/html/pages/pages/structure/layout/" 后的相对路径, 如 common/testlayout
	 * @return
	 */
	public static <T> T loadConfiguration(Structure structure, String folderRelativePath) {
		KeyAndValue<String, File> locationInfo = checkNotNull(getStructureLocationInfo(structure, folderRelativePath));
		
		try {
			return JAXBAnalysisHelper.<T>loadConfiguration(locationInfo.getKey(), 
					Files.newInputStreamSupplier(locationInfo.getValue()).getInput());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * B2B B2C 组件/布局 配置信息文件信息
	 * 
	 * @param structure
	 * @param relativePath
	 * @return
	 */
	private static KeyAndValue<String, File> getStructureLocationInfo(Structure structure, String relativePath) {
		KeyAndValue<String, File> location = new KeyAndValue<String, File>();

//		StringBuffer path = new StringBuffer(StringUtils.substringBefore(
//				JAXBAnalysisHelper.class.getResource("/").getPath(), "/html/"));
		
		String rootPath = JAXBAnalysisHelper.class.getResource("/").getPath();
		String webInfStr = "WEB-INF";
		String htmlStr = "html";
		String appendStr = "pages/structure/";
		StringBuffer result = new StringBuffer();
		if (StringUtils.contains(rootPath, webInfStr)) {
			result.append(StringUtils.substringBefore(rootPath, webInfStr));
		} else {
			result.append(StringUtils.substringBefore(rootPath, htmlStr));
		}
		result.append(appendStr);

		switch (structure) {
		case LAYOUT:
			location.setKey("com.desksoft.mall.structure.layout.jaxb");
			result.append("layout/").append(relativePath)
					.append("/layout-config.xml");
			break;
		case COMPONENT:
			location.setKey("com.desksoft.mall.structure.component.jaxb");
			result.append("component/").append(relativePath)
					.append("/com-config.xml");
			break;
		default:
			return null;
		}

		location.setValue(new File(result.toString()));

		return location;
	}

}
